<!--
 * @Author: daifen
 * @Date: 2020-06-01 11:58:32
 * @LastEditors: daifen
--> 
<template>
  <el-select
    :clearable="sClearable"
    :disabled="disabled"
    :class="sClass"
    v-model="svalue"
    @change="handleChangeSelect"
    :placeholder="sPlaceholder"
  >
    <el-option
      v-for="dict in distList"
      :key="dict.dictValue"
      :label="dict.dictLabel"
      :value="dict.dictValue"
    ></el-option>
  </el-select>
</template>

<script>
export default {
  name: "commonSelect",
  props: {
    value: {
      type: [String],
    },
    distType: {
      type: String,
      required: true,
    },
    keys: {
      type: String,
      default: "",
    },
    label: {
      type: String,
      default: "Text",
    },
    option: {
      type: String,
      default: "Value",
    },
    sClass: {
      type: String,
      default: "querySelect",
    },
    sClearable: {
      type: Boolean,
      default: true,
    },
    sPlaceholder: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    //传入一个新的值进去 可以为全部 或者其他 当不传递时默认不存在
    newVal: {
      type: String,
      defalut: "",
    },
  },
  data() {
    return {
      distList: [],
      svalue: this.value,
    };
  },
  created() {
    this.getDist(this.distType);
  },
  methods: {
    handleChangeSelect() {
      console.log(this.svalue);
      this.$emit("handleChangeSelect", this.svalue);
    },
    getDist(type) {
      this.getDicts(type).then((response) => {
        this.distList = response.data;
        if (!syUtiles.commonUtils.isEmpty(this.newVal)) {
          this.distList.unshift({ dictLabel: this.newVal, dictValue: "0" });
        }
      });
    },
  },
  watch: {
    value(newVal) {
      this.svalue = newVal;
    },
    svalue(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.$emit("input", this.svalue);
      }
    },
  },
};
</script>
